package com.yllt4cloud.common.ddbase.util.response;

import com.alibaba.fastjson.JSONObject;
import com.yllt4cloud.common.core.util.ResultResp;
import com.yllt4cloud.common.ddbase.constant.ApplicationContext;
import com.yllt4cloud.common.ddbase.constant.ErrorCode;
import com.yllt4cloud.common.ddbase.constant.entity.ParamException;
import com.yllt4cloud.common.ddbase.constant.entity.ServiceException;
import com.yllt4cloud.common.ddbase.constant.entity.SessionEntity;
import com.yllt4cloud.common.ddbase.util.JsonUtil;
import com.yllt4cloud.common.ddbase.util.ResponseUtils;
import com.yllt4cloud.common.ddbase.util.crypto.CryptoManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * 
 * <p><b>类名称：</b>ResponseUtil 工具类 </p>
 * <p><b>类描述：</b></p>
 * <p><b>日期：</b>2016-1-22 上午10:50:33</p>
 * @author yaozq
 */
public class ResponseUtil {

	private static final Logger logger = LoggerFactory.getLogger(ResponseUtil.class);
	/**
	 * 向界面写会数据
	 * @param response
	 * @param
	 */
	public static void out(HttpServletResponse response, ResultResp<?> res){
		PrintWriter out = null;
		response.setContentType("application/json;charset=UTF-8");
		try {
			out = response.getWriter();
			out.print(res.toString());
		} catch (Exception e) {
			logger.error("写回界面数据异常" + e.getMessage(), e);
		}finally{
			if(out != null){
				out.flush();
				out.close();
			}
		}
	}
	public static void renderText(HttpServletResponse httpServletResponse, ResultResp resultResp){
		SessionEntity sessionEntity= ApplicationContext.CTX.getSessionEntity();
		if(null!=sessionEntity&&sessionEntity.canEncrypt()) {
			String encryedResult = CryptoManager.getInstance().getCrypto(sessionEntity.getCryptEnum()).encrypt(JsonUtil.toJson(resultResp),sessionEntity.getEncryptKey());
			ResponseUtils.renderText((HttpServletResponse) httpServletResponse, encryedResult);
			return;
		}
		ResponseUtils.renderText((HttpServletResponse) httpServletResponse, JsonUtil.getShowJson(resultResp));
	}

	public static void resolveException(HttpServletRequest request,
										 HttpServletResponse response, Object object, Exception exception) {

		if(exception instanceof ServiceException){
			ServiceException serviceException = (ServiceException) exception;
			logger.error("ServiceException:" + JSONObject.toJSONString(serviceException.getResultResp()), exception);
			renderText(response, serviceException.getResultResp());
		} else if(exception instanceof ParamException){
			ParamException paramException = (ParamException) exception;
			logger.error("ParamException:" + JSONObject.toJSONString(paramException.getResultResp()), exception);
			renderText(response, paramException.getResultResp());
		} else{
			logger.error(exception.getMessage(), exception);
			renderText(response, ResultResp.fail("系统异常", ErrorCode.UNKNOW_ERROR));
		}
	}

	public static void processError(ServletResponse servletResponse, ResultResp resultResp) {
		renderText((HttpServletResponse)servletResponse, resultResp);
	}
}
